package com.shunda.ysdgj.manager.controller.goods;

import com.shunda.ysdgj.manager.service.goods.GoodsSpecService;
import com.shunda.ysdgj.manager.service.goods.GoodsSpuService;
import com.shunda.ysdgj.model.dto.base.IdsDto;
import com.shunda.ysdgj.model.dto.goods.GoodsSpecFindDto;
import com.shunda.ysdgj.model.entity.goods.GoodsSpec;
import com.shunda.ysdgj.model.entity.goods.GoodsSpecItem;
import com.shunda.ysdgj.model.vo.ResultVo;
import com.shunda.ysdgj.model.vo.goods.GoodsSpecVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "商品规格接口")
@RestController
@RequestMapping("/api/v1/admin/goods/spec")
@RequiredArgsConstructor
public class GoodsSpecController {

    private final GoodsSpuService goodsSpuService;
    private final GoodsSpecService goodsSpecService;

    @Operation(summary = "根据产品SPU获取商品规格")
    @GetMapping("getBySpu/{spuId}")
    public ResultVo getBySpu(@PathVariable Long spuId) {
        Map<String, Long> categoryIdMap = goodsSpuService.getSpecCategoryIdsById(spuId);
        Map<String, List<GoodsSpecVo>> resData = new HashMap<>();
        resData.put("searchSpecList", goodsSpecService.findByCategoryId(categoryIdMap.get("searchCategoryId")));
        resData.put("noSearchSpecList", goodsSpecService.findByCategoryId(categoryIdMap.get("noSearchCategoryId")));
        return ResultVo.success(resData);
    }

    @Operation(summary = "分页获取商品规格列表")
    @PostMapping("list")
    public ResultVo list(@RequestBody GoodsSpecFindDto goodsSpecFindDto) {
        return ResultVo.success(goodsSpecService.findByPage(goodsSpecFindDto));
    }

    @Operation(summary = "添加商品规格")
    @PostMapping("add")
    public ResultVo add(@RequestBody GoodsSpec goodsSpec) {
        if (!goodsSpecService.add(goodsSpec)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "修改商品规格")
    @PostMapping("modify")
    public ResultVo modify(@RequestBody GoodsSpec goodsSpec) {
        if (!goodsSpecService.modify(goodsSpec)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "删除商品规格")
    @PostMapping("delete")
    public ResultVo delete(@RequestBody IdsDto idsDto) {
        if (!goodsSpecService.deleteByIds(idsDto)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

}
